<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/lodash.js/4.17.21/lodash.min.js" type="application/javascript"></script>
</head>

<body></body>
<script>
  //原始数据格式
  let rows = [
    {
      id: "1063705989926227968",
      mobile: "13800000002",
      username: "管理员",
      password: "c8b7722b1139bb9b346409e503302e82",
      enableState: 1,
      createTime: "2018-11-17T16:10:45.000+0000",
      companyId: "1",
      companyName: "传智播客",
      departmentId: "1175310929766055936",
      timeOfEntry: "2018-11-02T08:00:00.000+0000",
      formOfEmployment: 1,
      workNumber: "9002",
      formOfManagement: null,
      workingCity: null,
      correctionTime: "2018-11-30T00:00:00.000+0000",
      inServiceStatus: 1,
      departmentName: "总裁办",
      level: "coAdmin",
      staffPhoto:
        "https://hr-project-80-1314002096.cos.ap-nanjing.myqcloud.com/1663810629494",
    },
    {
      id: "1066370498633486336",
      mobile: "13800000003",
      username: "孙财",
      password: "14af10ffa3798486632a79cbbf469376",
      enableState: 1,
      createTime: null,
      companyId: "1",
      companyName: "传智播客",
      departmentId: "1175311466846683136",
      timeOfEntry: "2018-11-04T08:00:00.000+0000",
      formOfEmployment: 1,
      workNumber: "111",
      formOfManagement: null,
      workingCity: null,
      correctionTime: "2018-11-20T00:00:00.000+0000",
      inServiceStatus: 1,
      departmentName: "市场部",
      level: "user",
      staffPhoto:
        "http://pkbivgfrm.bkt.clouddn.com/1066370498633486336?t=1545812322518",
    },
  ];
  //目标数据格式
  // 满足条件
  // (1)部分字段:姓名,手机号,入职日期,聘用形式,转正日期,工号,部门
  // (2)二维数组,存放值
  /*
  let data = [
    ["管理员","13800000002","2018-11-02T08:00:00.000+0000", '...'],
    ['...']
  ]
  */

  // lodash
  // 1. 选择需要的字段
  // let item =  _.pick(rows[0], ['username', 'mobile', 'timeOfEntry'])
  // console.log(item);
  // console.log(Object.keys(item));
  // console.log(Object.values(item));

  const headers = {
    '姓名': 'username',
    '手机号': 'mobile',
    '入职日期': 'timeOfEntry',
    '聘用形式': 'formOfEmployment',
    '转正日期': 'correctionTime',
    '工号': 'workNumber',
    '部门': 'departmentName'
  }
  // let data = rows.map(t => {
  //   // 1. 首先是从rows每个元素中过滤出headers对应的字段
  //   let tm = _.pick(t, Object.values(headers))
  //   console.log(tm);
  //   // 2. 过滤后的每个元素获取其values,Object.values返回为数组,最后map返回组成的新数组
  //   return Object.values(tm)
  // })
  // console.log(data);

  // 原生写法
  // let newA = Object.keys(rows[0]).filter(t => ['username', 'mobile', 'timeOfEntry'].includes(t))
  // console.log(newA);

  let newData = rows.map(t => {
    // ['mobile', 'username', 'timeOfEntry']
    let newA = Object.keys(t).filter(m => Object.values(headers).includes(m))
    return newA.map(m => t[m])
  })
  console.log(newData);
</script>

</html>
